package com.nand.module.function.ui;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.fastjson.JSON;
import com.lzy.okgo.model.HttpParams;
import com.nand.library.base.base.BaseActivity;
import com.nand.library.base.bean.ResponseBean;
import com.nand.library.base.http.MyCallback;
import com.nand.library.base.http.OkGoUtils;
import com.nand.library.base.utils.SpUtils;
import com.nand.library.zxing.activity.CaptureActivity;
import com.nand.library.zxing.activity.CodeUtils;
import com.nand.module.function.R;
import com.nand.module.function.bean.ClampingBean;
import com.nand.module.function.constant.URL;
import com.nand.module.function.utils.LogUtils;

import java.util.List;

/**
 * created by 86185
 * on 2020/2/25
 */
@Route(path = "/function/clamping/details")
public class ClampingDetalisActivity extends BaseActivity implements View.OnClickListener {

    private TextView mTvGzbm, mTvGzmc, mTvSbgs, mTvGys, mTvSm, mTvZsy, mTvDw, mTvWxcs, mTvYwxcs, mTvLy, mTvZt, mTvConfirm, mTvConfirm2, mTvConfirm3;
    private int type;
    private String fFixtureOid;
    private int userId;
    private String fixtureCode;
    private String fixtureName;
    private String userOid;
    private String fSupplierOid;
    private String repairOid;
    private String debugOid;
    private View viewLine, viewLine2;
    private LinearLayout mLlBottom, mLlBottom2;
    private TextView mTvLiftBottom, mTvRightBottom, mTvLiftBottom2, mTvRightBottom2;
    private String equipCode;
    private String equipName;
    private String repairer;
    private String repairSupplierCode;
    private LinearLayout mLlGys;
    private TextView mTvWxf;
    private TextView mTvGys2;
    private String lifeTime;
    private String usage;
    private String repairedAmount;
    private int repairAmount;
    private TextView mTvGyslift;

    @Override
    protected int initLayout() {
        return R.layout.activity_clamping_details;
    }

    @Override
    protected boolean isActionBar() {
        return true;
    }

    @Override
    protected void initView() {
        mActionBar().setCenterText("工装台账详情");
        type = getIntent().getIntExtra("type", 0);
        fFixtureOid = getIntent().getStringExtra("fFixtureOid");
        userId = SpUtils.readInt(ClampingDetalisActivity.this, "userId", 0);


        mTvGzbm = findViewById(R.id.tv_gzbm);
        mTvGzmc = findViewById(R.id.tv_gzmc);
        mTvSbgs = findViewById(R.id.tv_sbgs);
        mTvGys = findViewById(R.id.tv_gys);
        mTvSm = findViewById(R.id.tv_sm);
        mTvZsy = findViewById(R.id.tv_zsy);
        mTvDw = findViewById(R.id.tv_dw);
        mTvWxcs = findViewById(R.id.tv_wxcs);
        mTvYwxcs = findViewById(R.id.tv_ywxcs);
        mTvLy = findViewById(R.id.tv_ly);
        mTvZt = findViewById(R.id.tv_zt);
        viewLine = findViewById(R.id.view_line);
        viewLine2 = findViewById(R.id.view_line2);
        mLlBottom = findViewById(R.id.ll_bottom);
        mLlBottom2 = findViewById(R.id.ll_bottom2);
        mTvLiftBottom = findViewById(R.id.tv_lift_bottom);
        mTvRightBottom = findViewById(R.id.tv_right_bottom);
        mTvLiftBottom2 = findViewById(R.id.tv_lift_bottom2);
        mTvRightBottom2 = findViewById(R.id.tv_right_bottom2);
        mLlGys = findViewById(R.id.ll_gys);
        mTvWxf = findViewById(R.id.tv_wxf);
        mTvGys2 = findViewById(R.id.tv_gys2);
        mTvGyslift = findViewById(R.id.tv_gys2_lift);

        mTvConfirm = findViewById(R.id.tv_confirm);
        mTvConfirm2 = findViewById(R.id.tv_confirm2);
        mTvConfirm3 = findViewById(R.id.tv_confirm3);
        mTvConfirm.setOnClickListener(this);
        mTvConfirm2.setOnClickListener(this);
        mTvConfirm3.setOnClickListener(this);

        switch (type) {
            case 0: //在库
                mTvConfirm2.setVisibility(View.VISIBLE);
                mTvConfirm3.setVisibility(View.VISIBLE);
                mTvConfirm.setText("安装");
                mTvConfirm2.setText("维修");
                mTvConfirm3.setText("调试");
                break;
            case 1: //安装
                mTvConfirm.setText("解绑");
                mLlBottom.setVisibility(View.VISIBLE);
                mLlBottom2.setVisibility(View.VISIBLE);
                viewLine.setVisibility(View.VISIBLE);
                viewLine2.setVisibility(View.VISIBLE);
                break;
            case 2: //维修
                mTvConfirm.setText("退库");
                mLlGys.setVisibility(View.VISIBLE);
                mTvGyslift.setText("供应商：");
                break;
            case 3: //调试
                mTvConfirm.setText("退库");
                break;
 /*   case 1: //安装

        break;
    case 2: //维修

        break;
    case 3: //调试

        break;*/
        }
    }

    @Override
    protected void initData() {
        HttpParams param = new HttpParams();
        param.put("oid", fFixtureOid);
        LogUtils.e("avv=======" + fFixtureOid);
        OkGoUtils.getInstance().post(this, URL.QUERY_TB_EQ_FIXTURE, param, 1, true, new MyCallback() {
            @Override
            public void onSuccess(ResponseBean response, int id) {
                if (response.getCode() == 200) {
                    String data = response.getDataInfo().toString();
                    LogUtils.e("avv=======" + data);
                    List<ClampingBean> clampingBeans = JSON.parseArray(data, ClampingBean.class);
                    if (0 < clampingBeans.size()) {
                        ClampingBean bean = clampingBeans.get(0);

                        fixtureCode = bean.getFixtureCode();
                        fixtureName = bean.getFixtureName();
                        userOid = bean.getUserOid();
                        fSupplierOid = bean.getFSupplierOid();
                        repairOid = bean.getRepairOid();
                        debugOid = bean.getDebugOid();
                        repairer = bean.getRepairer();
                        repairSupplierCode = bean.getRepairSupplierCode();

                        switch (type) {
                            case 0: //在库
                                break;
                            case 1: //安装
                                equipCode = bean.getEquipCode();
                                equipName = bean.getEquipName();
                                mTvLiftBottom.setText("设备编码");
                                mTvRightBottom.setText(equipCode);
                                mTvLiftBottom2.setText("设备名称");
                                mTvRightBottom2.setText(equipName);
                                break;
                            case 2: //维修
                                String supplierName = bean.getSupplierName();
                                mTvGys2.setText(supplierName);
                                String repairer = bean.getRepairer();
                                switch (repairer){
                                    case "0":
                                        mTvWxf.setText("供应商");
                                        break;
                                    case "1":
                                        mTvWxf.setText("内部维修");
                                        findViewById(R.id.ll_bottom3).setVisibility(View.GONE);
                                        break;

                                }

                                break;
                            case 3: //调试
                                mTvConfirm.setText("退库");
                                break;
                        }

                        mTvGzbm.setText(fixtureCode);
                        mTvGzmc.setText(fixtureName);
                        mTvSbgs.setText(bean.getFBelongOid());
                        mTvGys.setText(bean.getSupplierCode());
                        lifeTime = bean.getLifeTime();
                        mTvSm.setText(lifeTime);
                        usage = bean.getUsage();
                        mTvZsy.setText(usage);
                        mTvDw.setText(bean.getUnit());
                        repairAmount = bean.getRepairAmount();
                        mTvWxcs.setText( repairAmount + "");
                        repairedAmount = bean.getRepairedAmount();
                        mTvYwxcs.setText(repairedAmount);
                        mTvLy.setText(bean.getSource() + "");

                        switch (type) {
                            case 0:
                                mTvZt.setText("在库");
                                break;
                            case 1:
                                mTvZt.setText("安装");
                                break;
                            case 2:
                                mTvZt.setText("维修");
                                break;
                            case 3:
                                mTvZt.setText("调试");
                                break;
                        }
                    }
                }
            }

            @Override
            public void onError(Throwable e, int id) {

            }
        });
    }

    private static final int REQUEST_CODE = 1001;
    private static final int REQUEST_CODE2 = 1005;

    @Override
    public void onClick(View view) {
        int id = view.getId();
        Intent intent = null;
        Bundle bundle = null;

        if (id == R.id.tv_confirm) {//按钮1
            switch (type) {
                case 0: //在库
                    if (usage.isEmpty()){
                        showToast("总使用无值，无法安装");
                        return;
                    }
                    int iLifeTime = Integer.parseInt(lifeTime);
                    int iUsage = Integer.parseInt(usage);
                    if (iUsage > iLifeTime){
                        showToast("总使用次数大于寿命次数，无法安装");
                        return;
                    }

                    if (repairedAmount.isEmpty()){
                        showToast("已维修次数无值，无法安装");
                        return;
                    }
                    int iRepairedAmount = Integer.parseInt(repairedAmount);
                    if (iRepairedAmount > repairAmount){
                        showToast("已维修次数大于维修次数，无法安装");
                        return;
                    }


                    intent = new Intent(ClampingDetalisActivity.this, CaptureActivity.class);
                    startActivityForResult(intent, REQUEST_CODE);
                    break;
                case 1: //安装
                    intent = new Intent(ClampingDetalisActivity.this, ClampingCompileActivity.class);
                    bundle = new Bundle();
                    bundle.putString("fixtureCode", fixtureCode);//工装编码
                    bundle.putString("fixtureName", fixtureName); //工装名称
                    bundle.putString("equipCode", equipCode); //设备编码
                    bundle.putString("equipName", equipName); //设备名称
                    bundle.putString("fSupplierOid", fSupplierOid);
                    bundle.putString("fFixtureOid", fFixtureOid);
                    bundle.putString("userOid", userOid); //安装标识
                    bundle.putInt("type", type);
                    bundle.putInt("code", 1);
                    intent.putExtras(bundle);
                    startActivityForResult(intent, REQUEST_CODE2);
                    break;
                case 2: //维修
                    intent = new Intent(ClampingDetalisActivity.this, ClampingCompileActivity.class);
                    bundle = new Bundle();
                    // bundle2.putString("equipID", result);
                    bundle.putString("fixtureCode", fixtureCode);//工装编码
                    bundle.putString("fixtureName", fixtureName); //工装名称
                    bundle.putString("repairOid", repairOid);
                    bundle.putString("fSupplierOid", fSupplierOid);
                    bundle.putString("fFixtureOid", fFixtureOid);
                    bundle.putString("repairer", repairer);
                    bundle.putString("repairSupplierCode", repairSupplierCode);
                    bundle.putInt("type", type);
                    bundle.putInt("code", 2);
                    intent.putExtras(bundle);
                    startActivityForResult(intent, REQUEST_CODE2);
                    break;
                case 3: //调试

                    intent = new Intent(ClampingDetalisActivity.this, ClampingCompileActivity.class);
                    bundle = new Bundle();
                    // bundle2.putString("equipID", result);
                    bundle.putString("fixtureCode", fixtureCode);//工装编码
                    bundle.putString("fixtureName", fixtureName); //工装名称
                    bundle.putString("debugOid", debugOid);
                    bundle.putString("fFixtureOid", fFixtureOid);
                    bundle.putInt("type", type);
                    bundle.putInt("code", 3);
                    intent.putExtras(bundle);
                    startActivityForResult(intent, REQUEST_CODE2);
                    break;
            }

        } else if (id == R.id.tv_confirm2) {//按钮2
            Intent intent2 = new Intent(ClampingDetalisActivity.this, ClampingCompileActivity.class);
            Bundle bundle2 = new Bundle();
            // bundle2.putString("equipID", result);
            bundle2.putString("fixtureCode", fixtureCode);//工装编码
            bundle2.putString("fixtureName", fixtureName); //工装名称
            bundle2.putString("userOid", userOid);
            bundle2.putString("fSupplierOid", fSupplierOid);
            bundle2.putString("fFixtureOid", fFixtureOid);//工装标识
            bundle2.putInt("type", type);
            bundle2.putInt("code", 2);
            intent2.putExtras(bundle2);
            startActivityForResult(intent2, REQUEST_CODE2);
        } else if (id == R.id.tv_confirm3) {//按钮3
            HttpParams param = new HttpParams();
            param.put("fFixtureOid", fFixtureOid);
            param.put("creator", userId);
            OkGoUtils.getInstance().post(this, URL.FIXTURE_DEBUG, param, 1, true, new MyCallback() {
                @Override
                public void onSuccess(ResponseBean response, int id) {
                    if (200 == response.getCode()) {
                      /*  Intent intent = new Intent();
                        setResult(REQUEST_CODE2, intent);*/
                        showToast("调试成功");
                        // finish();
                        type = 3;
                        Intent intent = new Intent();
                        setResult(RESULT_OK, intent);

                        HttpParams param = new HttpParams();
                        param.put("oid", fFixtureOid);
                        OkGoUtils.getInstance().post(ClampingDetalisActivity.this, URL.QUERY_TB_EQ_FIXTURE, param, 1, true, new MyCallback() {
                            @Override
                            public void onSuccess(ResponseBean response, int id) {
                                if (response.getCode() == 200) {
                                    String data = response.getDataInfo().toString();
                                    LogUtils.e("avv=======" + data);
                                    List<ClampingBean> clampingBeans = JSON.parseArray(data, ClampingBean.class);
                                    if (0 < clampingBeans.size()) {
                                        ClampingBean bean = clampingBeans.get(0);
                                        debugOid = bean.getDebugOid();
                                    }
                                }
                            }

                            @Override
                            public void onError(Throwable e, int id) {

                            }
                        });
                        mTvConfirm.setText("退库");
                        mTvConfirm2.setVisibility(View.GONE);
                        mTvConfirm3.setVisibility(View.GONE);
                        mTvZt.setText("调试");
                    }
                }

                @Override
                public void onError(Throwable e, int id) {

                }
            });

        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        LogUtils.e("requestCode==============" + requestCode+"===="+resultCode);
        if (requestCode == REQUEST_CODE) {
            //处理扫描结果（在界面上显示）
            if (null != data) {
                final Bundle bundle = data.getExtras();
                if (bundle == null) {
                    return;
                }
                if (bundle.getInt(CodeUtils.RESULT_TYPE) == CodeUtils.RESULT_SUCCESS) {
                    final String result = bundle.getString(CodeUtils.RESULT_STRING);
                    // final String oid = bundle.getString(CodeUtils.RESULT_STRING2);
                    LogUtils.e("22222result===" + result);

                    HttpParams params = new HttpParams();
                    params.put("equipID", result);
                    OkGoUtils.getInstance().post(this, URL.EFFECT_EQUIPID, params, 1, false, new MyCallback() {
                        @Override
                        public void onSuccess(ResponseBean response, int id) {
                            if (200 == response.getCode()) {
                                Intent intent2 = new Intent(ClampingDetalisActivity.this, ClampingCompileActivity.class);
                                Bundle bundle2 = new Bundle();
                                bundle2.putString("equipID", result);
                                bundle2.putString("fixtureCode", fixtureCode); //工装编码
                                bundle2.putString("fixtureName", fixtureName); //工装名称
                                bundle2.putString("fFixtureOid", fFixtureOid); //工装标识
                                bundle2.putInt("type", type);
                                bundle2.putInt("code", 1);
                                intent2.putExtras(bundle2);
                                startActivityForResult(intent2, REQUEST_CODE2);

                            } else {
                                LogUtils.e("122222result===2");
                                showToast("设备编码不正确");
                            }
                        }

                        @Override
                        public void onError(Throwable e, int id) {
                        }
                    });
//                    Toast.makeText(this, "解析结果:" + result, Toast.LENGTH_LONG).show();

                } else if (bundle.getInt(CodeUtils.RESULT_TYPE) == CodeUtils.RESULT_FAILED) {
                    Toast.makeText(ClampingDetalisActivity.this, "解析二维码失败", Toast.LENGTH_LONG).show();
                }
            }
        } else if (requestCode == REQUEST_CODE2 && 0 != resultCode ) {
            Intent intent = new Intent();
            setResult(RESULT_OK, intent);
            finish();
        }
    }
}
